package com.example.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.domain.TransferClass;
import com.example.result.Result;
import com.example.service.TransferClassService;
import com.example.tool.GsonTool;

/**
 * @author 袁小弘
 * @describe 转班控制层
 * @date 2020-02-18
 */
@RestController
@RequestMapping("/api")
public class TransferClassController {

    @Autowired
    TransferClassService transferClassService;

    //===============================转班管理==============================================

    /**
     * @param json
     * @return 添加成功则提示成功信息，反之则提示失败信息
     * @describe 添加转班信息
     */
    @RequestMapping(value = "/transfers", method = RequestMethod.POST)
    public Result addTransferClass(@RequestBody String json) {
        //将JSON数据转换为TransferClass对象实体
        JSONObject parseObject = JSON.parseObject(json);
        TransferClass transferClass = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"), TransferClass.class);

        return transferClassService.addTransferClass(transferClass);

    }

    /**
     * @param json
     * @return 更新成功则提示成功信息，反之则提示失败信息
     * @describe 更新转班信息
     */
    @RequestMapping(value = "/transfers", method = RequestMethod.PUT)
    public Result updateTransferClass(@RequestBody String json) {
        //将JSON数据转换为TransferClass对象实体
        JSONObject parseObject = JSON.parseObject(json);
        TransferClass transferClass = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"), TransferClass.class);
        return transferClassService.updateTransferClass(transferClass);

    }

    /**
     * @param transferClassId
     * @return 删除成功则提示成功信息，反之则提示失败信息
     * @describe 根据转班编号删除转班信息
     */
    @RequestMapping(value = "/transfers/{transferClassId}", method = RequestMethod.DELETE)
    public Result deleteTransferClassById(@PathVariable("transferClassId") String transferClassId) {
        return transferClassService.deleteTransferClassById(transferClassId);

    }

    /**
     * @return 以JSON格式返回所有班级信息
     * @describe 查询所有转班信息
     */
    @RequestMapping(value = "/transfers", method = RequestMethod.GET)
    public Result queryAllTransferClass() {
        return transferClassService.queryAllTransferClass();

    }


    /**
     * @param transferClassId
     * @return 以JSON格式返回该转班信息
     * @describe 根据编号查询转班信息
     */
    @RequestMapping(value = "/transfers/{transferClassId}", method = RequestMethod.GET)
    public Result queryTransferClassById(@PathVariable("transferClassId") String transferClassId) {
        return transferClassService.queryTransferClassById(transferClassId);

    }

    /**
     * @param json
     * @return 添加成功则返回一个成功提示，反之则提示失败信息
     * @describe 通过Excel批量添加转班信息
     */
    @RequestMapping(value = "/transfers/excels", method = RequestMethod.POST)
    public Result addTransferClassByExcel(@RequestBody String json) {

        JSONObject parseObject = JSON.parseObject(json);
        List<TransferClass> list = JSON.parseArray(parseObject.getString("json"), TransferClass.class);


        return transferClassService.addTransferClassByExcel(list);

    }

    /**
     * @param Ids
     * @return 删除成功则返回一个成功提示，反之则提示失败信息
     * @describe 根据转班编号批量删除转班信息
     */
    @RequestMapping(value = "/transfers/dels", method = RequestMethod.POST)
    public Result deleteTransferClassByIds(@RequestBody Map<String, String> Ids) {

        return transferClassService.deleteTransferClassByIds(Ids);
    }

    /**
     * @param json
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 转班信息模糊查询
     */
    @RequestMapping(value = "/transfers/serachs", method = RequestMethod.POST)
    public Result serachByInfo(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        String[] info = parseObject.getString("json").split(",");

        return transferClassService.serachByInfo(info);

    }

    /**
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 查询所有用户转班信息
     */
    @RequestMapping(value = "/transfers/transferClassAndUsers", method = RequestMethod.GET)
    public Result queryAllTransferClassAndUsersInfo() {
        return transferClassService.queryAllTransferClassAndUsersInfo();

    }


    /**
     * @param json
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 根据学生ID查询学生转班信息
     */
    @RequestMapping(value = "/transfers/transferClassByUserId", method = RequestMethod.POST)
    public Result selectTransferClassByUserId(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        Integer userId = Integer.parseInt(parseObject.getString("json"));

        return transferClassService.selectTransferClassByUserId(userId);
    }
}
